/******************************************************************************
** 
** Reverse strings.
** 
** File name:	reverse.c
** Compilation:	cc reverse.c -o reverse
** Execution:	./reverse
** 
** Description:
** Reads the input, divides it into strings and prints the reversed strings. 
** 
******************************************************************************/
#include <stdio.h>

#define MAX_LINE_LENGTH 1000

int get_line(char line[], int max_length);
void reverse_line(char line[], int length);
void copy_line(char source[], char dest[]);


int main()
{
    int current_length;
    char current_line[MAX_LINE_LENGTH];

    current_length = 0;
    while ((current_length = get_line(current_line, MAX_LINE_LENGTH)) != 0) {
        reverse_line(current_line, current_length);
        printf("%s\n", current_line);
    }

    return 0;
}


// get_line: Reads one line from the input and stores it in the array, returns line length.
int get_line(char line[], int max_length) {
    int c, i;

    for (i=0; i<max_length && (c=getchar()) != EOF && c != '\n'; ++i) {
        line[i] = c;
    }
    if (c == '\n') {
        line[i] = c;
        ++i;
    }
    line[i] = '\0';
    return i;
}


// reverse_line: Gets the string and its length as an argument and reverses the string (except '\n' and '\0' of course).
void reverse_line(char line[], int length) {
    int i, k;
    char reversed_line[length];
    
    k = 0;
    for (i=length; i>=0; i--) {
        if ((line[i] != '\n') && (line[i] != '\0')) {
            reversed_line[k] = line[i];
            ++k;
        }
    }
    reversed_line[k] = '\n';
    ++k;
    reversed_line[k] = '\0';
    
    copy_line(reversed_line, line);
}


// copy_line: Copies one char array to another.
void copy_line(char source[], char dest[]) {
    int i;

    i = 0;
    while ((dest[i] = source[i]) != '\0') {
      ++i;
    }
}
                        